<?php
namespace app\index\controller;
use think\Controller;
use think\view;
use think\Db;
use think\Session;
use think\Request;
class Wxjs extends Controller
{

    function show($status, $message, $data = array())
    {
        $reuslt = array(
            'status' => $status,
            'message' => $message,
            'data' => $data,
        );
        exit(json_encode($reuslt));   //输出一条信息 并退出当前脚本
    }
    /**
     * 短信发送
     */
    public function msg(){
        if($_POST){
            $phone  = $_POST['phone'];
            $code  =rand(1000,9999);
            $msg= '您的注册验证码是 '. $code .'，请勿泄露给他人！';
            $res = 	$this->SendSms_Demo_Curl($phone,$msg,"");

            if($res)
            {
                //将手机号和验证码存储
                $data['phone'] =$phone;
                $data['code'] = $code;
                $data['uptime'] = date('Y-m-d H:i:s',time());
                Db::name('phone')->insert($data);
                $this->show(1,'发送成功');
            }else{
                $this->show(0,'发送失败');
            }

        }
    }
    /**
     * 发送短信接口
     */
    function SendSms_Demo_Curl($mobile,$Msg,$longnum){
        $post_data = array();
        $post_data['username'] = "lpcs02";
        $post_data['password'] = "123123";
        $post_data['longnum'] = 9082;
        $post_data['phonelist'] = $mobile;
        $post_data['msg'] = $Msg;
        $url='http://101.200.228.238/NewSmsPort/default.asmx/SendSms';
        $o="";
        foreach ($post_data as $k=>$v)
        {
            $o.= "$k=".urlencode($v)."&";
        }
        $post_data=substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出，返回到变量
        //为了支持cookie
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        curl_close($ch);//关闭

        if((int)strip_tags($result)>0){
            return strip_tags(substr($result,0,strrpos($result,'|')));   //返回发送短信成功数
        }else{
            return false;
        }
    }

    /**
     * 登录接口
     */
    public function login()
    {

        $map['phone']  = $_POST['phone'];

        $password = $_POST['password'];

        $ret = Db::name('developer')->where($map)->find();
        if(!$ret){
            return $this->show(0,'该用户不存在');
        }
        if($ret['state']==0){
            return $this->show(0,'该用户已被禁用，请联系管理员');
        }
        if($ret['state']==-1){
            return $this->show(0,'该用户审核未通过，请联系管理员');
        }
        if($ret['state']==1){
            return $this->show(0,'正在审核中，请稍后再试');
        }
        if(($ret['password'])!=(md5($password))){
            return  $this->show(0,'密码错误');
        }


        $ret['password'] = '';
        if ($ret) {
            $this->show(1, '登录成功',$ret);
        } else {
            $this->show(0, '登录失败');
        }

    }

    /**
     * 技术注册接口
     */
    public function reg()
    {

        $phone = $_POST['phone'];

        $msgCode  =$_POST['msgCode'];
        $msg  = Db::name('phone')->where(array(
            'phone'=>$phone,
            'code'=>$msgCode
        ))
            ->find();

        if(!$msg){
            $this->show(0,'验证码错误');
        }



        $user = Db::name('developer')->where(array(
            'phone' => $phone
        ))->find();
        if ($user) {
            $this->show(0, '该手机号已注册，请更换手机号');
        }

        //获取openID
        $code = $_POST['code'];
        $curl = curl_init();
        //配置appid
        $appid = "wxaeae5ec2124342f4";
        //配置appscret
        $secret = "ff35487505508d87adf9e680e8bd1198";
        //api接口
        $url =    'https://api.weixin.qq.com/sns/jscode2session?appid='.$appid.'&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';


        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

        $str = curl_exec($curl);

        //释放curl句柄
        curl_close($curl);


//        print_r($str);
        $str = json_decode($str,true);
        $openid = $str['openid'];

        if (!$openid) {
            $this->show(0, '未获取到微信用户信息，请重试');
        }

        $_POST['uptime'] = date('Y-m-d H:i:s', time());
        $_POST['token'] = time();
        $_POST['password'] = md5($_POST['password']);
        $_POST['state'] = 1;  //刚注册的状态
        $_POST['openid'] = $openid;

        $add = Db::name('developer')->insert($_POST);
        if ($add) {
            $this->show(1, '注册成功，请登录', $_POST['token']);
        } else {
            $this->show(0, '注册失败，请重试');
        }


    }

    /**
     * 修改个人信息接口
     */
    public function edit()
    {
        $header = Request::instance()->header();
        $token = $header['token'];
        $user = DB::name('developer')
            ->field('id')
            ->where(array(
                'token'=>$token
            ))->find();
        $_POST['uptime'] = date('Y-m-d H:i:s',time());
        $res = Db::name('developer')->where(array(
            'id'=>$user['id']
        ))->update($_POST);
        if ($res) {
            $this->show(1, '修改成功');
        } else {
            $this->show(0, '修改失败');
        }
    }


    /**
     * 请求用户信息接口
     */
    public function get_developer()
    {

        $header = Request::instance()->header();
        $token = $header['token'];

        $res = Db::name('developer')->where(array(
            'token' => $token,

        ))
            ->find();
        $res['password'] = '';

        //将tip还原为json
//        $tip = explode(",", $res['tip']);
//        $tip = json_encode($tip);
//        $res['tip'] = $tip;
        if ($res) {
            $this->show(1, '获取成功', $res);
        } else {
            $this->show(1, '未获取到数据');
        }
    }


    /**
     * 技术获取待接单订单列表
     */
    public function get_order_list()
    {
        $page = $_POST['page'];
        $pageSize = $_POST['pageSize'];
        $res = Db::name('order')
            ->field('id,num,name,language,completeTime,state,content')
            ->where(array(
                'state' => '待接单'
            ))
            ->order('uptime desc')
            ->page($page,$pageSize)
            ->select();
        $res1['list'] = $res;
        $res1['sum']  = Db::name('order')
            ->where(array(
                'state' => '待接单'
            ))
            ->count();
        $res1['page_num']  = $page;
        if ($res) {
            $this->show(1, '获取成功', $res1);
        } else {
            $this->show(1, '未获取到数据');
        }
    }

    /**
     * 通过单号获取订单详细信息
     */
    public function get_order_byid()
    {

        $res = Db::name('order')
            ->field('id,state,num,uptime,completeTime,developerFee,name,language,platform,industry,field,max,min,content,beizhu,first_fee_state,end_fee_state,content')
            ->where(array(
                'id' => $_POST['id'],
                'state' => '待接单'
            ))
            ->find();
        //获取file
        $file = db::name('file')->where(array(
            'order_id'=>$res['id']
        ))->select();
        $res['file'] =json_encode($file) ;
        if ($res) {
            $this->show(1, '获取成功', $res);
        } else {
            $this->show(1, '未获取到数据');
        }
    }

    /**
     * 获取我的订单列表
     */
    public function my_order()
    {
        $header = Request::instance()->header();
        $token = $header['token'];
        $user = Db::name('developer')
            ->field('id')
            ->where(array(
                'token' => $token,
            ))->find();
        $page = $_POST['page'];
        $pageSize = $_POST['pageSize'];
        $res = Db::name('order')
            ->field('id,num,name,language,completeTime,state,first_fee_state,end_fee_state,content')
            ->where(array(
                'developerId' => $user['id'],
                'state' => $_POST['type']
            ))
            ->order('uptime desc')
            ->page($page,$pageSize)
            ->select();
        $res1['list'] = $res;
        $res1['sum']  = Db::name('order')
            ->where(array(
                'developerId' => $user['id'],
                'state' => $_POST['type']
            ))
            ->count();
        $res1['page_num']  = $page;

        if ($res) {
            $this->show(1, '获取成功', $res1);
        } else {
            $this->show(1, '未获取到数据');
        }

    }

    /**
     * 获取我的订单 单条数据信息
     */
    public function my_order_byid(){
        $header = Request::instance()->header();
        $token  = $header['token'];
        $user = Db::name('developer')
            ->field('id')
            ->where(array(
                'token'=>$token,
            ))->find();
        $res = Db::name('order')
            ->field('id,state,num,uptime,completeTime,developerFee,name,language,platform,industry,field,max,min,content,beizhu,first_fee_state,end_fee_state,apply_first_time,apply_end_time,content,deliver')
            ->where(array(
                'developerId'=>$user['id'],
                'id'=>$_POST['id']
            ))
            ->find();

        if($res){
            $file = db::name('file')->where(array(
                'order_id'=>$res['id']
            ))->select();
            $res['file'] =json_encode($file) ;
            $this->show(1,'获取成功',$res);
        }else{
            $this->show(1,'未获取到数据');
        }
    }


    /**
     * 技术申请首款接口
     */
     public function get_first(){

         $data['apply_first_time'] = date('Y-m-d H:i:s',time());
         $data['first_fee_state'] = '首款已申请';
         $res = Db::name('order')
             ->field('id,first_fee_state,apply_first_time')
             ->where(array(
             'id'=>$_POST['id'],
         ))->update($data);
         if ($res) {
             $this->show(1, '申请成功');
         } else {
             $this->show(0, '申请失败');
         }
     }
    /**
     * 技术申请尾款接口
     */
    public function get_end(){

        $data['apply_end_time'] = date('Y-m-d H:i:s',time());
        $data['end_fee_state'] = '尾款已申请';
        $res = Db::name('order')
            ->field('id,end_fee_state,apply_end_time')
            ->where(array(
            'id'=>$_POST['id'],
        ))->update($data);
        if ($res) {
            $this->show(1, '申请成功',$res);
        } else {
            $this->show(0, '申请失败');
        }
    }


    /**
     * 返回当前订单 待接单 已接单 已结算 等的数量
     */
    public function my_statistics(){
        $header = Request::instance()->header();
        $token  = $header['token'];
        $user = Db::name('developer')
            ->field('id')
            ->where(array(
                'token'=>$token,
            ))->find();
        $res['on'] = Db::name('order')
            ->where(array(
                'developerId'=>$user['id'],
                'state'=>'开发中'
            ))
            ->count();
        $res['after'] = Db::name('order')
            ->where(array(
                'developerId'=>$user['id'],
                'state'=>'售后中'
            ))
            ->count();
        $res['finish'] = Db::name('order')
            ->where(array(
                'developerId'=>$user['id'],
                'state'=>'已完成'
            ))
            ->count();
        $res['total'] = Db::name('order')
            ->where(array(
                'developerId'=>$user['id'],
            ))
            ->where('state','in','开发中,售后中,已完成')
            ->count();
        $res['developerFee'] = Db::name('order')
            ->where(array(
                'developerId'=>$user['id'],
                'state'=>'已完成'
            ))
            ->sum('developerFee');


        if($res){

            $this->show(1,'获取成功',$res);
        }else{
            $this->show(1,'未获取到数据');
        }
    }


}